import React, { Component } from 'react'
import ClassSon from './components/ClassSon'
import Son from './components/Son'

export default class App extends Component {
   
    state = {
        count:10
    }
    // 1. 父组件中定义方法
    getAge(age){
        console.log('App age: ', age)
    }

    // 1. 父组件中定义方法
    addCount(num){
        console.log('this: ', this)
        // 修改count
        this.setState({
            count: this.state.count + num
        })
    }
    render() {
        console.log('App run')
        return (
            <>
                <h3>App</h3>
                <p>App count: {this.state.count}</p>
                <p><button onClick={()=>{
                    this.setState({
                        count:this.state.count + 1
                    })
                }}>count++</button></p>

                <hr />
                {/*2. 将方法传递给子组件*/}
                <Son count={this.state.count} getAge={this.getAge}  addCount={this.addCount.bind(this)}/>

                <ClassSon  count={this.state.count} getAge={this.getAge} addCount={this.addCount.bind(this)}/>
            </>
        )
    }
}
